
import { 
  HTTP 
} from "../utils/http.js"

class classicModel extends HTTP {
  getlatest(resolve){
    this.request({
      url: 'classic/latest',
      success: (res)=>{
        wx.setStorageSync('classic-'+ res.data.index,res) 
        resolve(res)
      }
      }
    )
  }

  getClassic(index,resolve,nexOrPrevious){
    let key = nexOrPrevious == 'next'? 
              this._getKey(index + 1 ):
              this._getKey(index - 1 )        
    let res = wx.getStorageSync(key)
    if(!res){
      this.request({
        url: 'classic/'+index+'/'+nexOrPrevious,
        success: (res)=>{
          wx.setStorageSync(key,res)
          resolve(res)
        }
      })
    } else {
      resolve(res)
    }       
  }

  isFalst(index){
    return index == 1 ? false : true
  }

  isLstest(index){
    let latestIndex = this._getLatestindex()
    return latestIndex == index ? false : true
  }

  _setLatestindex(index){
    wx.setStorageSync('latestIndex',index)
  }
  _getLatestindex(){
    return wx.getStorageSync('latestIndex')
  }
  _getKey(key){
    return 'classic-' + key
  }
}

export { classicModel }
